import 'package:freezed_annotation/freezed_annotation.dart';

class SafeIntConverter extends JsonConverter<int, dynamic> {
  final int defaultValue;

  const SafeIntConverter({this.defaultValue = 0});

  @override
  int fromJson(dynamic json) {
    if (json is int) {
      return json;
    }
    if (json is String) {
      try {
        return int.parse(json);
      } catch (e) {
        return defaultValue;
      }
    }
    return defaultValue;
  }

  @override
  dynamic toJson(int object) {
    return object;
  }
}
